package com.admission.service;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.xml.sax.SAXException;

import com.admission.dao.NoticeDao;
import com.admission.model.Notice;
import com.admission.util.PageUtils;
import com.admission.util.XmlUtils;

@Repository
@Transactional
public class NoticeManager {
	private NoticeDao noticeDao;

	/**
	 * 创建主页xml
	 * 
	 * @throws IllegalArgumentException
	 * @throws SAXException
	 * @throws IllegalAccessException
	 * @throws IOException
	 */
	public void createXml() {
		Page<Notice> noticePage = new Page<Notice>(PageUtils.MAIN_ENROLL);
		Page<Notice> noticeNew = new Page<Notice>(PageUtils.MAIN_ENROLL);
		Page<Notice> noticeRead = new Page<Notice>(PageUtils.MAIN_ENROLL);
		// 最新导读
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p = new PropertyFilter("EQS_latestReading",
				Notice.READINGTRUE);
		filters.add(p);
		noticeNew.setOrder(Page.DESC);
		noticeNew.setOrderBy("inputTime");
		searchNotice(noticeNew, filters);
		// 招办公告
		noticePage.setOrder(Page.DESC);
		noticePage.setOrderBy("inputTime");
		getPage(noticePage);
		// 最新公告
		List<PropertyFilter> filters2 = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p2 = new PropertyFilter("EQS_latestAnnouncement",
				Notice.ANNOUNCEMENTTRUE);
		filters2.add(p2);
		noticeRead.setOrder(Page.DESC);
		noticeRead.setOrderBy("inputTime");
		searchNotice(noticeRead, filters2);

		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		path += "/other/mainpage-noticePage.xml";
		XmlUtils.create(noticePage.getResult(), path);

		path = ServletActionContext.getServletContext().getRealPath("/WEB-INF");
		path += "/other/mainpage-noticeNew.xml";
		XmlUtils.create(noticeNew.getResult(), path);

		path = ServletActionContext.getServletContext().getRealPath("/WEB-INF");
		path += "/other/mainpage-noticeRead.xml";
		XmlUtils.create(noticeRead.getResult(), path);

	}

	@Autowired
	public void setNoticeDao(NoticeDao noticeDao) {
		this.noticeDao = noticeDao;
	}

	public void saveNotice(Notice entity) {
		noticeDao.save(entity);
	}

	public Page<Notice> getPage(Page<Notice> page) {
		return noticeDao.findPage(page);
	}

	@Transactional(readOnly = true)
	public Page<Notice> searchNotice(final Page<Notice> page,
			final List<PropertyFilter> filters) {
		return noticeDao.findPage(page, filters);
	}

	public Notice getById(String id) {
		return noticeDao.findUniqueBy("id", id);
	}

	public void deleteNotcieByIds(String ids) {
		for (String id : ids.split(",")) {
			noticeDao.delete(id.trim());
		}
	}
}
